package com.flxz.forum.service.impl;

import java.util.List;
import com.flxz.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.flxz.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.flxz.forum.domain.ForumReply;
import com.flxz.forum.mapper.ForumTopicMapper;
import com.flxz.forum.domain.ForumTopic;
import com.flxz.forum.service.IForumTopicService;

/**
 * 讨论主题Service业务层处理
 * 
 * @author fl
 * @date 2025-09-14
 */
@Service
public class ForumTopicServiceImpl implements IForumTopicService 
{
    @Autowired
    private ForumTopicMapper forumTopicMapper;

    /**
     * 查询讨论主题
     * 
     * @param topicId 讨论主题主键
     * @return 讨论主题
     */
    @Override
    public ForumTopic selectForumTopicByTopicId(Long topicId)
    {
        return forumTopicMapper.selectForumTopicByTopicId(topicId);
    }

    /**
     * 查询讨论主题列表
     * 
     * @param forumTopic 讨论主题
     * @return 讨论主题
     */
    @Override
    public List<ForumTopic> selectForumTopicList(ForumTopic forumTopic)
    {
        return forumTopicMapper.selectForumTopicList(forumTopic);
    }

    /**
     * 新增讨论主题
     * 
     * @param forumTopic 讨论主题
     * @return 结果
     */
    @Transactional
    @Override
    public int insertForumTopic(ForumTopic forumTopic)
    {
        forumTopic.setCreateTime(DateUtils.getNowDate());
        int rows = forumTopicMapper.insertForumTopic(forumTopic);
        insertForumReply(forumTopic);
        return rows;
    }

    /**
     * 修改讨论主题
     * 
     * @param forumTopic 讨论主题
     * @return 结果
     */
    @Transactional
    @Override
    public int updateForumTopic(ForumTopic forumTopic)
    {
        forumTopic.setUpdateTime(DateUtils.getNowDate());
        forumTopicMapper.deleteForumReplyByTopicId(forumTopic.getTopicId());
        insertForumReply(forumTopic);
        return forumTopicMapper.updateForumTopic(forumTopic);
    }

    /**
     * 批量删除讨论主题
     * 
     * @param topicIds 需要删除的讨论主题主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteForumTopicByTopicIds(Long[] topicIds)
    {
        forumTopicMapper.deleteForumReplyByTopicIds(topicIds);
        return forumTopicMapper.deleteForumTopicByTopicIds(topicIds);
    }

    /**
     * 删除讨论主题信息
     * 
     * @param topicId 讨论主题主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteForumTopicByTopicId(Long topicId)
    {
        forumTopicMapper.deleteForumReplyByTopicId(topicId);
        return forumTopicMapper.deleteForumTopicByTopicId(topicId);
    }

    /**
     * 新增讨论回复信息
     * 
     * @param forumTopic 讨论主题对象
     */
    public void insertForumReply(ForumTopic forumTopic)
    {
        List<ForumReply> forumReplyList = forumTopic.getForumReplyList();
        Long topicId = forumTopic.getTopicId();
        if (StringUtils.isNotNull(forumReplyList))
        {
            List<ForumReply> list = new ArrayList<ForumReply>();
            for (ForumReply forumReply : forumReplyList)
            {
                forumReply.setTopicId(topicId);
                list.add(forumReply);
            }
            if (list.size() > 0)
            {
                forumTopicMapper.batchForumReply(list);
            }
        }
    }
}
